/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.gui;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import javax.swing.event.*;
import javax.swing.table.*;
import model.*;

/**
 *
 * @author Bro
 */
public class TableTrack extends AbstractTableModel {

    private ArrayList tracks = new ArrayList();
    private Set<TableModelListener> listeners = new HashSet<TableModelListener>();
    public ArrayList columns = null;
    
    public String getColumnName(int columnIndex) {
        return columns.get(columnIndex).toString();
    }

    public Class<?> getColumnClass(int columnIndex) {
        return columns.get(columnIndex).getClass();
    }

    public int getColumnCount() {
        return columns.size();
    }

    public void addTableModelListener(TableModelListener listener) {
        listeners.add(listener);
    }

    public void removeTableModelListener(TableModelListener listener) {
        listeners.remove(listener);
    }

    public int getRowCount() {
        return tracks.size();
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        Track temp = (Track) tracks.get(rowIndex);
        switch (columnIndex) {
            case 0:
                return rowIndex + 1;
            case 1:
                return temp.getName();
            case 2:
                return temp.getArtist();
            case 3:
                return temp.getAlbum();
            case 4:
                return temp.getGenre();
            case 5:
                return temp.getLength();
            case 6:
                return temp.getSize();
        }
        return "";
    }

    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }

    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        Track t = (Track) tracks.get(rowIndex);
        switch (columnIndex) {
            case 1:
                t.setName((String) aValue.toString());
                return;
            case 2:
                t.setArtist(new Artist(aValue.toString()));
                return;
            case 3:
                t.setAlbum(new Album(aValue.toString()));
                return;
            case 4:
                t.setGenre(new Genre(aValue.toString()));
                return;
            case 5:
                t.setLength(Integer.parseInt((String) aValue));
                return;
            case 6:
                t.setSize(Integer.parseInt((String) aValue));
                return;
        }
    }

    public TableTrack(ArrayList items, ArrayList columns) {
        tracks = items;
        this.columns = columns;
    }

    public Track getSelectedItem(int index) {
        return (Track) tracks.get(index);
    }
}